package edu.cdnu.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.cdnu.common.dto.ChapterRecordDto;
import edu.cdnu.common.dto.VisitTimeDto;
import edu.cdnu.common.result.ResultEntity;
import edu.cdnu.pojo.CataloguePojo;
import edu.cdnu.pojo.CourseStudentPojo;
import edu.cdnu.pojo.ResourcePojo;
import edu.cdnu.pojo.StudentPojo;
import edu.cdnu.service.CatalogueService;
import edu.cdnu.service.CourseService;
import edu.cdnu.service.ResourceService;
import edu.cdnu.service.StudentService;

/**
 * 统计信息的controller
 * @author ASUS AsingZ
 */
@RestController
@RequestMapping("/record")
public class RecordDataController {
		
	@Autowired
	private CatalogueService catalogueService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private CourseService courseService;
	
	/**
	 * 资源下载次数
	 * @param courseId
	 * @return
	 */
	@GetMapping("/data/{courseId}")
	public ResultEntity<?> recordDataList(@PathVariable Integer courseId){
		List<Integer> catalogueIdList=catalogueService.selectCatalogueListByCourseIdAndState(courseId, 2);
		List<ResourcePojo> list=resourceService.selectResourceListByCatalogueIdList(catalogueIdList);
		
		return ResultEntity.createSuccessResult(list);
	}
	
	/**
	 * 章节学习次数
	 * @param courseId
	 * @return
	 */
	@GetMapping("/chapter/{courseId}")
	public ResultEntity<?> chapterStudyTime(@PathVariable Integer courseId){
		List<ChapterRecordDto> list=new ArrayList<>();
		List<CataloguePojo> catalogueList=catalogueService.selectLeafCatalogueListByCourseIdAndState(courseId, 1);
		catalogueList.forEach(item->{
			ResourcePojo resourcePojo=new ResourcePojo();
			resourcePojo.setCatalogueId(item.getId());
			ResourcePojo resourcePojo1=resourceService.selectResourceByResourcePojo(resourcePojo);
			list.add(
				new ChapterRecordDto(
					item.getId(), item.getCatalogueText(), item.getCatalogueOrder(), 
					item.getParentId(), item.getState(), item.getCourseId(), 
					resourcePojo1==null||resourcePojo1.getDownload()==null?0:resourcePojo1.getDownload()
				)
			);
		});

		return ResultEntity.createSuccessResult(list);
	}
	
	/**
	 * 访问次数
	 * @param courseId
	 * @return
	 */
	@GetMapping("/visitTime/{courseId}")
	public ResultEntity<?> studentVisitTime(@PathVariable Integer courseId){
		List<VisitTimeDto> list=new ArrayList<>();
		List<CourseStudentPojo> courseStudentLis=courseService.selectCourseStudentListByCourseId(courseId);
		List<Integer> studentIdList=new ArrayList<>();
		courseStudentLis.forEach(item->{
			studentIdList.add(item.getStudentId());
		});
		List<StudentPojo> studentList=studentService.selectStudentListByIdList(studentIdList);
		
		courseStudentLis.forEach(item->{
			studentList.forEach(studentItem->{
				if(item.getStudentId()==studentItem.getId()) {
					list.add(new VisitTimeDto(studentItem.getName(), item.getVisitTime()));
				}
			});
		});
		
		return ResultEntity.createSuccessResult(list);
	}
	
}
